package com.logindemo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.logindemo.domain.Account;
import com.logindemo.domain.LoginAccount;
import com.logindemo.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    private AccountRepository accountRepository;
//    @Autowired
//    private MenuService menuService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<Account> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Account::getUsername, username);
        Account account = accountRepository.selectOne(lambdaQueryWrapper);
        if( account == null){
           return null;
        }
//        List<String> collect = menuService.getAllMenu().stream().map(s -> s.getPath()).collect(Collectors.toList());
        return new LoginAccount(account, null);
    }
}
